package com.jichangxiu.framework.action;

import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.utils.FileUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * FileAction:
 *
 * @author Jcx
 * @create 2023-12-15 19:21
 */
@Rest("/file")
@Tag(name = "系统-文件管理控制器")
public class FileAction {

    @Limit
    @Repeat
    @Log(remark = "平台-上传图片接口")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "平台-上传图片接口")
    @Post(value = "/uploadImage", removeBlank = false)
    public Record uploadImage(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), Constants.JCX_PROPERTIES.getFileProperties().getImageSuffix());
        return FileUtils.saveFile(file, Constants.JCX_PROPERTIES.getFileProperties().getImageFolder());
    }

    @Limit
    @Repeat
    @Log(remark = "平台-上传视频接口")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "平台-上传视频接口")
    @Post(value = "/uploadVideo", removeBlank = false)
    public Record uploadVideo(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), Constants.JCX_PROPERTIES.getFileProperties().getVideoSuffix());
        return FileUtils.saveFile(file, Constants.JCX_PROPERTIES.getFileProperties().getVideoFolder());
    }

    @Limit
    @Repeat
    @Log(remark = "平台-上传音频接口")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "平台-上传音频接口")
    @Post(value = "/uploadAudio", removeBlank = false)
    public Record uploadAudio(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), Constants.JCX_PROPERTIES.getFileProperties().getAudioSuffix());
        return FileUtils.saveFile(file, Constants.JCX_PROPERTIES.getFileProperties().getAudioFolder());
    }

    @Limit
    @Repeat
    @Log(remark = "平台-上传安卓安装包接口")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "平台-上传安卓安装包接口")
    @Post(value = "/uploadApk", removeBlank = false)
    public Record uploadApk(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), Constants.JCX_PROPERTIES.getFileProperties().getApkSuffix());
        return FileUtils.saveFile(file, Constants.JCX_PROPERTIES.getFileProperties().getApkFolder());
    }

    @Limit
    @Repeat
    @Log(remark = "平台-上传文件接口")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "平台-上传文件接口")
    @Post(value = "/uploadFile", removeBlank = false)
    public Record uploadFile(MultipartFile file) {
        // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
        FileUtils.checkFileSize(file);
        FileUtils.checkSuffix(file.getOriginalFilename(), Constants.JCX_PROPERTIES.getFileProperties().getFileSuffix());
        return FileUtils.saveFile(file, Constants.JCX_PROPERTIES.getFileProperties().getFileFolder());
    }

    @Limit
    @Repeat
    @Log(remark = "平台-多文件上传接口")
    @PreAuthorize("isAuthenticated()")
    @Operation(summary = "平台-多文件上传接口")
    @Post(value = "/uploadFiles", removeBlank = false)
    public List<Record> uploadFiles(List<MultipartFile> files) {
        List<Record> records = new ArrayList<>();
        for (MultipartFile file : files) {
            // 验证文件大小 -> 验证后缀 -> 保存到硬盘 -> 地址返回给前端
            FileUtils.checkFileSize(file);
            FileUtils.checkSuffix(file.getOriginalFilename(), Constants.JCX_PROPERTIES.getFileProperties().getFileSuffix());
            records.add(FileUtils.saveFile(file, Constants.JCX_PROPERTIES.getFileProperties().getFileFolder()));
        }
        return records;
    }

}
